package web.dao;

import web.util.DateUtil;
import web.util.StringOperateUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 * Created by Administrator on 2016/7/2.
 */
@Repository
public class SearchDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;  //新猫数据库

    @Autowired
    @Qualifier("jdbcTemplateYF")
    private JdbcTemplate jdbcTemplateYF;  //研发数据库

    private static final Logger LOGGER = Logger.getLogger("SearchDao.class");
    /**
     * 通过关键词查询研发中标单位
     * @param table
     * @param key
     * @param set
     */
    public void findWinBidCompany(String table,String key,final Set<String> set){
        String sql="select win_bid_company from "+table+
                " where match(content_fc)against(? in boolean mode) and win_bid_company <> '' and status=0 ";
        jdbcTemplateYF.query(sql, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                if(!set.contains(rs.getString("win_bid_company"))){
                      set.add(rs.getString("win_bid_company"));
                }
            }

        },key);
    }

    /**
     * 查询中标所有相关数据表格
     * @return
     */
    public List<String> findTable(String key){
        final List<String> list=new ArrayList<String>();
        String sql="select TABLE_NAME as name from information_schema.`TABLES` where TABLE_NAME Like (?)";
        jdbcTemplateYF.query(sql, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                list.add(rs.getString("name"));
            }
        },key);
        return list;
    }

    /**
     * 通过中标单位查询中标公司联系方式
     * @param winBidCompany
     * @return
     */
    public Map<String,Object> findWinContactByCompany(final String winBidCompany){
//        System.out.println(Charset.defaultCharset());
        String sql="select com_phone,cellphone,email from supplier_contact where md5 = ?";
        final Map<String,Object> map=new HashMap<String, Object>();
        jdbcTemplate.query(sql, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                map.put("com_phone", rs.getString("com_phone"));
                if(rs.getString("com_phone")!=null){
                    map.put("com_phone",rs.getString("com_phone").replaceAll(",", "，"));
                }
                map.put("cellphone",rs.getString("cellphone"));
                if(rs.getString("cellphone")!=null){
                    map.put("cellphone",rs.getString("cellphone").replaceAll(",", "，"));
                }
                map.put("email",rs.getString("email"));
                if(rs.getString("email")!=null){
                    map.put("email",rs.getString("email").replaceAll(",", "，"));
                }
            }
        }, winBidCompany);
        return map;
    }



    /**
     * 通过关键词语表明查询招标单位
     * @param key
     * @param table
     * @return
     */
    public List<String> findSubscriberByKey(String key,String table){
        String sql = "select subscriber from "+table+ " where match(content_fc) against ('"
                + StringOperateUtil.returnKey(key)+"' in boolean mode) and status = 0 and subscriber != ''" +
                " and stime > ?";
        final List<String> list = new ArrayList<String>();
        jdbcTemplateYF.query(sql, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                list.add(rs.getString("subscriber"));
            }
        }, DateUtil.getNowToInt()-(60 * 60 * 24 * 30 * 3));
        return list;
    }

    /**
     * 通过招标单位查询标题
     * @param subscriber
     * @param table
     * @return
     */
    public List<String> findTitleBySubscriber(String subscriber,String table){
        String sql = "select title from "+table+" where match(subscriber_fc) against('"
                + StringOperateUtil.returnKey(subscriber)+"' in boolean mode) and status = 0 and stime > ? ";
        final List<String> list = new ArrayList<String>();
        jdbcTemplateYF.query(sql, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                list.add(rs.getString("title"));
            }
        }, DateUtil.getNowToInt()-(60 * 60 * 24 * 30 * 3));
        return list;
    }
}
